import { nextTick, ref, watch } from "vue";
import { useI18n } from "/@/locales/useI18n";
import type { SearchModel } from "/@/api/models";

export function useEmptyText(options?: { ignoreKeys?: string[] }) {
  const emptyText = ref("");
  const modelForEmptyText = ref<SearchModel>();
  const { t } = useI18n();
  watch(
    modelForEmptyText,
    (v) => {
      if (v) {
        if (v.searchText) {
          emptyText.value = t("common.notFound");
          return;
        } else {
          const ignoreKeys = options?.ignoreKeys || [];
          for (let key in v) {
            if (v[key] !== undefined && v[key] !== null && v[key] !== "" && !ignoreKeys.includes(key)) {
              emptyText.value = t("common.notFound");
              return;
            }
          }
        }
      }
      emptyText.value = "";
    },
    {
      deep: true,
    }
  );
  return {
    emptyText,
    modelForEmptyText,
  };
}
